<template>
  <div class="center-index-report">
    <dv-loading v-if="loading"></dv-loading>
    <Indexanalysis
      v-else
      :tableList="tableList"
      :tableTitles="tableTitles"
      :optionsData="optionsData"
      :selectData="selectData"
      @Checkyear="checkyear"
      @ConfirmQuery="confirmQuery"
      @ConfirmExport="confirmExport"
      @Selectindex="selectindex"
      :valueIndex="valueIndex"
    />
  </div>
</template>
<script>
import Indexanalysis from "../../components/indexanalysis/Indexanalysis.vue";
import {
  queryStationBox,
  queryStationRightForm,
  queryStationLeftForm,
  stationLeftExport,
} from "../../api/base8090.js";
import { download } from "../../utils/index.js";
import { mapState } from "vuex";
export default {
  name: "indexanalysis",
  components: {
    Indexanalysis,
  },

  data() {
    return {
      loading: true,
      timer: null,
      tableList: [],
      tableTitles: [],
      roseData: [],
      optionsData: {},
      selectData: [],
      valueIndex: "",
      queryParmas: {
        stationCode: sessionStorage.getItem("stationCode"),
        year: new Date().getFullYear() + "",
        uid: null,
      },
    };
  },
  created() {
    this.timer = setInterval(() => {
      if (document.readyState === "complete") {
        this.loading = false;
        window.clearInterval(this.timer);
      }
    }, 1000);
    queryStationBox({
      stationCode: this.queryParmas.stationCode,
    }).then((res) => {
      this.selectData = res.data;
      this.valueIndex = res.data[0].uid;
      this.queryParmas.uid = res.data[0].uid;
      queryStationLeftForm(this.queryParmas).then((res) => {
        if (res.code == 200) {
          this.tableTitles = res.data[0];
          let arr = res.data;
          arr.splice(0, 1);

          let item = [];
          arr.forEach((res) => {
            let newObject = {};
            res.forEach((item) => {
              newObject[item.tag] = item.value;
            });
            item.push(newObject);
          });
          this.tableList = item;
        }
      });
      queryStationRightForm(this.queryParmas).then((res) => {
        if (res.code == 200) {
          this.optionsData = res.data.data;
        }
      });
    });
  },
  methods: {
    initData() {
      this.tableList = [];
      this.tableTitles = [];
      this.optionsData = {};

      queryStationLeftForm(this.queryParmas).then((res) => {
        if (res.code == 200) {
          this.tableTitles = res.data[0];
          let arr = res.data;
          arr.splice(0, 1);

          let item = [];
          arr.forEach((res) => {
            let newObject = {};
            res.forEach((item) => {
              newObject[item.tag] = item.value;
            });
            item.push(newObject);
          });
          this.tableList = item;
        }
      });
      queryStationRightForm(this.queryParmas).then((res) => {
        if (res.code == 200) {
          this.optionsData = res.data.data;
        }
      });
    },
    selectindex(uid) {
      this.valueIndex = uid;
      this.queryParmas.uid = uid;
    },
    checkyear(year) {
      this.queryParmas.year = year;
      this.initData();
    },
    // 查询
    confirmQuery() {
      this.initData();
    },
    //导出
    confirmExport() {
      stationLeftExport(this.queryParmas).then((res) => {
        if (res) {
          var fileName;
          if (this.stationName) {
            fileName = this.stationName;
          } else {
            fileName = this.deptName;
          }

          download(
            res,
            "xls",
            "12号线" + fileName + this.queryParmas.year + "年指标报表"
          );
          this.$message.success("导出成功");
        } else {
          this.$message.error("导出失败");
        }
      });
    },
  },
  computed: {
    ...mapState(["stationCode", "stationName", "deptName"]),
  },
  watch: {
    stationCode(newVal, oldVal) {
      if (newVal != oldVal) {
        this.queryParmas.stationCode = newVal;
        this.initData();
      }
    },
  },
};
</script>
<style lang="less" scoped>
.center-index-report {
  width: 100%;
  height: 100%;
}
</style>
